package com.aynu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.aynu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102400749163";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgU/ZnVA1qWNeoCKETnpV3p18DG84+2O09QPCMzo5+I39lmI2bpcI3BHxs6fpBSGoxdaWMq35ungsgVegvYJqdr3PWWq6nNFowQrF3nBUhDqNE+M6Cv8+1WBsqQPIiySWUNHLKfsLH2gsfYaXIYDuSK8TfpBGvHdWJ2yU6poieEv/be47K/FM5vjqWusdtRea4fGQXpJkHC7IlEhI/p0kYJYH1SmET1pDGnGR1arlfaXABGZ5GLeAf1eX4nE20AnGEVB/h1KKZ+Cs5xi35ouhpXXCXuRmmcYOr4C/4zkGIfFa3eIVffFp/LSPFhJz2db0JsChzrXspkqj474pqlQhRAgMBAAECggEAJ7tG9+pTfMRRhTnGOSd2oPVgTVPps5dsi155flt94nKHkrTQev8D90BsWU1OkDkS3/jossiZB26xES1Y4czN5D/js/47BAKsSwNsGVkbv1pySZhjDPHd1nHkIX+AwfAZGPY6cEYjnQVC9l7rh5GZkaq5Gjb9jukUzxn4yuUUPB/SuY7PRvJJ/1jHC5s/jerxBB1sidKbAxjQA/09JOPOs8DR53YKpteOYTbemocSXm5U4M2PGKXaCBHnacsMwbLYb+otlOLuJ3Z7Ll6fQwDEJuJ/0tqxNSjZRroTIlI0ILoLfBKTnR9zXCsrqmhAQ5PhWNTM9LZfIwOI79fIdbvmAQKBgQDTWBL/a1phQThEFHg2mRgPfhSzsD2brFgLBEfQIrBpqyYamT8+jlXrkNklr+or7dZf7vwzaUBGKBSOKiQSE8bApd88zzse+ByE0hQ+uARSN4UXxpGOhN6SVP7LzV9PHpg4jILOmqVLTNF8pfPRBniusOWdlB+ZH2f6rFM6iYACMQKBgQDCNFnHAOgGyHuFnHfzd+mzPcVzSdxoXwH5rgjVZYk4DMZRLVsImAk5ff+md5vOwX7VowNtMU5XZ1bsFwBxxjos8KMMOkwqpXUmzzH61Pu9t4x8e23wK+3aug2Tc36bnDfgeKMHRes2Sz0fkmGr9TgquVmzmH3KsHOJSGQA2nDAIQKBgQCYXmor7ajWbx/yrVNSAaQAH2Ure0efgWxkXOytWSUYucSZ7pxTLGkzkVWMvzwz+Z/RwEnhguNM8T6IlEc3JTzozW2ZuMol9LkS5SalZSPwZoi08HsmZhLVKyQbqPAq6aLTOjJgY8wB8tshY7/ZljHRuEWDCzSQv7zGVBEZJeK9MQKBgBsB4MJWE+5tjxADtIK9WFi+7GieCvLQ05t/Ymca2bxbunCrmA9X5WCDbzwdQiO8sQDQzLcwti5dhq8Ke9tWr9x0EpiuuszreaFOCytQ1zmI/xaIq79MvKVAXob09tR5IyQqYrGuPYmtaPCtELwzO/5lDF53hHbdlxOuZ4ne1vNBAoGBAMBjYMk0jXwr4Tu+7FK+jc3ESrQiq0OjqCMbRfNfUvT9ZuaJQBv9NelS4Tki67s3o5XqfeZ+eDdobuRYoMc7FxaBRSxUzd/v5rUDrg61GsSTN1oAyYvg6Z/oU+QkbJM8xqQ0VT2yl+jhmDOz4k22zI5Ck/77PZweIlkMsJwM7eEG\n";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiByF+QkpQJjqIs3Cc/57rxmdGauSLYCuND0t6qMpBSVE+QTJ4Bxv9JRoVp19vcUE0wHaEdCdO2ntx1j2KZGKBHgbcwhQSWyzoElPPvv8CvaKyRK964o69j1qtB3SqBzBC2zx6kKuzvwy/xyCuPVpftAbjdC4eA8OHMl/M4w0NN1EJgjOx1KZsP//RR5UuvTbobc48mmzA5R8jGZjod3A/aihQWmAraFURhBBTN6Wvh7TffN4rmV4y3/DKLWlhc+SV7o3Kq+AvoJDf3Wx003sJFHtxGZ+xo0Hns9Xb+PnZoco83Xfisi/sDxSNx0kS09KHZ0rjUIrOxK590bGGZQ/zwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://gulimallmj.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //支付超时
    private String timeOut = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeOut +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
